/*
 *功能： 用户信息 处理 
 */
package softdone;

import java.sql.*;
import java.util.Collection;
import java.util.ArrayList;

import softdata.Use_data;
import softdone.Conn;

public class Use_done{
	public int Num_Page;		//当前列表用户的总数
	public Collection UserList;
	public Use_data userdata=new Use_data();
	public boolean Che_NameR(String s){
		boolean CHECK=false;
		try{
			String sql="{ call user_reg_checkn(?,?) }";
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setString(1, s);
			cs.registerOutParameter(2,java.sql.Types.INTEGER);
			cs.execute();
			if(cs.getInt(2)==0) CHECK=true;
			//如果用户名没有注册，返回true
			}catch(Exception e){
				e.printStackTrace();
			}
		return CHECK;
	}
	public boolean Che_EmailR(String s){
		boolean CHECK=false;
		try{
			String sql="{ call user_reg_checke(?,?) }";
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setString(1, s);
			cs.registerOutParameter(2,java.sql.Types.INTEGER);
			cs.execute();
			if(cs.getInt(2)==0) CHECK=true;
			// 如果Email没有注册，返回true
			}catch(Exception e){
				e.printStackTrace();
			}
		return CHECK;
	}
	public String GetPho(int id){
		String Pho=null;
		try{
			String sql="{ call user_getpho(?,?) }";
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1,id);
			cs.registerOutParameter(2,java.sql.Types.VARCHAR);
			cs.execute();
			Pho= cs.getString(2);
			}catch(Exception e){
				e.printStackTrace();
			}
			return Pho;
	}
	public int RegisterR(String Name,String Pwd,String NickName,String Email,String Birthday,int Sex,String Photo){
		int I=0;
		String sql="{ call user_insert(?,?,?,?,?,?,?,?) }";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.registerOutParameter(1,java.sql.Types.INTEGER);
			cs.setString(2, Name); 
			cs.setString(3, Pwd); 
			cs.setString(4, NickName);
			cs.setString(5, Email);
			cs.setString(6,Birthday);
			cs.setInt(7,Sex);
			cs.setString(8,Photo);
			System.out.println(1);
			cs.execute(); 
			System.out.println(2);
			I=cs.getInt(1);
			System.out.println(3);
			Dataconn.CloseCC();
			}catch(Exception e){
				e.printStackTrace();
			}
		return I;
	}
	
	public int Che_Login(String Name,String Pwd){
		int I=0;
		String sql="{ call user_login(?,?,?) }";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setString(1, Name); 
			cs.setString(2, Pwd); 
			cs.registerOutParameter(3,java.sql.Types.INTEGER);
			cs.execute(); 
			I=cs.getInt(3);
			Dataconn.CloseCC();
			}catch(Exception e){
				e.printStackTrace();
			}
		return I;
	}
	public void Use_delete(int id){
		
	}
	
	public void Use_Read(int id){
		String sql="{ call user_read(?) }";
		System.out.println(id);
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1,id);
			ResultSet rs=cs.executeQuery(); 
			if(rs.next()){
			userdata.ID=rs.getInt(1);
			userdata.Name=rs.getString(2);
			userdata.Pwd=rs.getString(3);
			userdata.Nickname=rs.getString(4);
			userdata.Email=rs.getString(5);
			userdata.Birthday=rs.getString(6);
			userdata.Sex=rs.getInt(7);
			userdata.Photo=rs.getString(8);
			userdata.Remarks=rs.getString(9);
			userdata.date_r=rs.getString(10);
			userdata.Las_login=rs.getString(12);
			userdata.Integral=rs.getInt(13);
			}
			Dataconn.CloseCC();
			}catch(Exception e){
				e.printStackTrace();
			}
	}
	public void Use_Edit(int id){
		String sql="{ call user_edit(?,?,?,?,?,?,?) }";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1, id);
			cs.setString(2,userdata.Pwd);
			cs.setString(3,userdata.Nickname);
			cs.setInt(4,userdata.Sex);
			cs.setString(5,userdata.Birthday);
			cs.setString(2,userdata.Photo);
			cs.setString(2,userdata.Remarks);
			cs.execute();
			Dataconn.CloseCC();
			}catch(Exception e){
				e.printStackTrace();
			}
	}
	public int Num_User(){
		int I=0;
		String sql="{ call user_sum(?) }";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.registerOutParameter(1,java.sql.Types.INTEGER);
			cs.execute(); 
			I=cs.getInt(1);
			Dataconn.CloseCC();
			}catch(Exception e){
				e.printStackTrace();
			}
		return I;
	}
	public void Use_List_Read(int page){
		// 管理员板块读取用户信息表
		String sql="{ call user_list_read(?) }";
		try{
			Num_Page=0;
			UserList=new ArrayList();
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1, page);
			ResultSet rs=cs.executeQuery(); 
			while(rs.next()){
			userdata=new Use_data();
			userdata.ID=rs.getInt(1);
			userdata.Name=rs.getString(2);
			userdata.Pwd=rs.getString(3);
			userdata.Nickname=rs.getString(4);
			userdata.Email=rs.getString(5);
			userdata.Photo=rs.getString(6);
			userdata.date_r=rs.getString(7);
			userdata.Integral=rs.getInt(8);
			UserList.add(userdata);
			Num_Page++;
			}
			rs.close();
			Dataconn.CloseCC();
			}catch(Exception e){
				e.printStackTrace();
			}
	}
	public void Use_List_Edit(int id,String pwd,String nick,String email){
		// 管理员板块读取用户信息表
		String sql="{ call user_list_edit(?,?,?,?) }";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setInt(1, id);
			cs.setString(2,pwd);
			cs.setString(3,nick);
			cs.setString(4,email);
			cs.execute();
			Dataconn.CloseCC();
			}catch(Exception e){
				e.printStackTrace();
			}
	}
	public String getName(int id){
		/*
		 * 
		 */
		String Nickname=null;
		String sql="{ call getname(?,?) }";
		try{
		Conn Dataconn=new Conn();
		Dataconn.Create();
		CallableStatement cs=Dataconn.CSt_SQL(sql);
		cs.setInt(1, id); 
		cs.registerOutParameter(2,java.sql.Types.CHAR);
		cs.execute(); 
		Nickname=cs.getString(2);
		Dataconn.CloseC();
		}catch(Exception e){
			e.printStackTrace();
		}
	return Nickname;
	}
	public int getIntegrate(int id){
		/*
		 * 根据用户id 查询积分
		 */
		int J=0;
		String sql="{ call user_read_jifen(?,?) }";
		try{
		Conn Dataconn=new Conn();
		Dataconn.Create();
		CallableStatement cs=Dataconn.CSt_SQL(sql);
		cs.setInt(1, id); 
		cs.registerOutParameter(2,java.sql.Types.INTEGER);
		cs.execute(); 
		J=cs.getInt(2);
		Dataconn.CloseC();
		}catch(Exception e){
			e.printStackTrace();
		}
		return J;
	}
	
	public int getId(String Name1){
		int Id=0;
		String sql="{ call get_id(?,?) }";
		try{
			Conn Dataconn=new Conn();
			Dataconn.Create();
			CallableStatement cs=Dataconn.CSt_SQL(sql);
			cs.setString(1, Name1); 
			cs.registerOutParameter(2,java.sql.Types.INTEGER);
			cs.execute(); 
			Id=cs.getInt(2);
			Dataconn.CloseC();
			}catch(Exception e){
				e.printStackTrace();
			}
		return Id;
	}
}